
package com.kanjian.music.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Feed;
import com.kanjian.music.entity.Music;
import com.kanjian.music.util.PlayerUtil;

/**
 * 邂逅ListAdapter
 * 
 * @author xuyanjun@kanjian.com
 */
public class FeedListAdapter extends MusicSubFragmentListAdapater<Feed> {

    private BaseActivity mActivity;
    
    public FeedListAdapter(Context context, ListView listView, BaseActivity activity) {
        super(context);
        this.mActivity = activity;
        registerOnItemClickListener(listView);
    }

    private void registerOnItemClickListener(ListView listview) {
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Feed feed = getItem(position);
                if (feed != null) {
                    if (feed.mFeedType == Feed.FEEDTYPE_FOLLOW_MUSICIAN) {
                        Intent intent = new Intent();
                        intent.putExtra(IntentConstants.MUSICIANID, feed.mUserId);
                        intent.setClass(mActivity, MusicianHomeActivity.class);
                        mActivity.startActivityWithAnimation(intent);
                    } else {
                        Music music = new Music();
                        music.setUserId(feed.mUserId);
                        music.setIsFav(feed.mIsFav);
                        music.mGenre = feed.mGenre;
                        music.setMusicName(feed.mMusicName);
                        music.setMusicUrl(feed.mMuiscUrl);
                        music.setMusicianLocation(feed.mUserLocation);
                        music.setPlayTime(feed.mPlayTime);
                        music.setMusicId(feed.mMusicId);
                        music.setUserPic(feed.mUserPic);
                        music.setUserName(feed.mUserName);
                        PlayerUtil.addMusicThenPlay(music, mActivity.mPlayService);
                    }
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_FAKE;
        } else {
            return VIEW_TYPE_DATA;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        super.getView(position, convertView, parent);
        if (getItemViewType(position) == VIEW_TYPE_FAKE) {
            return mListFakeView;
        } else {
            ViewHolder holder;
            if (convertView == null || convertView.getTag() == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_list_feed, null);
                holder.mTv_feedTitle = (TextView) convertView.findViewById(R.id.item_list_feed_title);
                holder.mBtn_play = (Button) convertView.findViewById(R.id.item_list_feed_play_btn);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            final Feed feed = getItem(position);
            holder.mTv_feedTitle.setText(feed.getTimeDifferent() + feed.getActionString());
            holder.mBtn_play.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlayerUtil.addMusicThenPlay(feed.mNewSong, mActivity.mPlayService);
                }
            });
            if (feed.mFeedType == Feed.FEEDTYPE_FOLLOW_MUSICIAN) {
                holder.mBtn_play.setVisibility(View.VISIBLE);
            } else {
                holder.mBtn_play.setVisibility(View.GONE);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView mTv_feedTitle;
        Button mBtn_play;
    }

}
